<?php
header ( "Content-Type: text/html; charset=utf-8" );
include "common.php";
class Login extends Common {
    const IMGPATH = 'uploads/';
	function __construct() {
		parent::__construct ();
		$this->load->database ();
	}
	// 默认页
	function index() {

        $data['data'] = $this->getHuanDeng(6);
		$this->load->view ( 'login', $data);
	}
	// 登录
	function logon() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$loginName = $this->input->post ( "phone" );
		$loginPwd = $this->input->post ( "pwd" );
		if ($loginName == "" || $loginPwd == "") {
			$arr ["Msg"] = "用户名和密码不能为空";
			echo json_encode ( $arr );
			exit ();
		}
		$query = $this->Muser->checkLogin ( $loginName, $loginPwd );
		foreach ( $query as $row ) {
			//$this->session->set_userdata ( 'email', empty ( $row->email ) ? "" : $row->email );
			//$this->session->set_userdata ( 'phone', $row->phone );
			$this->session->set_userdata ( 'id', $row->id );
			//$this->session->set_userdata ( 'realname', $row->realname );
			$arr ["ReturnCode"] = 1;
			echo json_encode ( $arr );
			exit ();
		}
		$arr ["Msg"] = "用户名或密码有误";
		echo json_encode ( $arr );
	}
	function logout() {
		$this->session->unset_userdata ( 'id' );
		$this->session->unset_userdata ( 'userinfo' );
        $jump_url =  base_url()."index.php/login/login";
        redirect($jump_url);
	}
	function loginsuccess() {

		$this->load->view ( 'regsuccess' );
	}
	// 手机注册页面
	function registerp() {
		$this->load->view ( 'registerp' );
	}
	function registerpsubmit() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$phone = $this->input->post ( 'phone' );
		$pwd = $this->input->post ( 'pwd' );
		$realname = '';
		$smscode = $this->input->post ( 'code' );
		$sex = '';
		$num = $this->Muser->updateLogin ( $phone, $pwd, $realname, $smscode, $sex );
		/*
		 * if(num<=0){ $arr["Msg"] = "注册失败"; echo json_encode($arr); exit; }
		 */
		$query = $this->Muser->checkLogin ( $phone, $pwd );
		foreach ( $query as $row ) {
			//$this->session->set_userdata ( 'email', isset ( $row->email ) ? $row->email : "" );
			//$this->session->set_userdata ( 'phone', $row->phone );
			$this->session->set_userdata ( 'id', $row->id );
			//$this->session->set_userdata ( 'realname', $row->realname );
		}
        $jump_url =  base_url()."index.php/login/loginsuccess";
        redirect($jump_url);
	}
	function registerEmailSubmit(){
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$email = $this->input->post ( 'email' );
		$pwd = $this->input->post ( 'pwd' );
		$realname = '';
		$sex = '';
		
		$guid = $this->create_guid();
		$num = $this->Muser->saveEmailLogin ( $email, $pwd, $realname, $sex,$guid );

		//$url = trim($_SERVER['HTTP_HOST'],"/")."/index.php/login/registeractivate?email=$email&checkcode=$guid";
		$content = $this->getRegEmailContent($email,$guid);
		
		$this->sendMail($email,$content);
        $e = explode("@",$email);
        $checkUrl = $this->getCheckEmailUrl($e[1]);

        $jump_url =  base_url()."index.php/login/loginemail?email={$email}&checkcode={$guid}&url={$checkUrl}";
        redirect($jump_url);
	}

	function getRegEmailContent($email,$guid){
		$url = base_url()."index.php/login/registeractivate?email=$email&checkcode=$guid";
		
		
		$content = "欢迎注册麻豆系统，此邮件为系统自动发送，请勿回复!<br/>
				   若非本人操作请忽略<br />
				 若是本人操作，请点击以下链接进行激活(或将其复制到浏览器的地址栏进行激活)<br />
				<a href='$url'>$url</a>";
		return $content;
	}
	function getCheckEmailUrl($domain){
		$url = $this->Muser->getCheckEmailUrl($domain);
		return $url;
	}
	function registeractivate(){
		$email = $_GET['email'];
		$checkcode = $_GET['checkcode'];
		$return = $this->Muser->registerActivate($email,$checkcode);
		//$result ="激活失败，可能是已激活,<a href='".base_url()."'>请登录</a>";
		if(!empty($return)){
            $this->session->set_userdata ( 'id', $return['id']);
        }
        $jump_url =  base_url()."index.php/login/loginsuccess";
        redirect($jump_url);
		
	}
	// 邮箱注册页面
	function registere() {
		$this->load->view ( 'registere' );
	}
	//邮箱注册
	function checkEmailForRegister(){
		$arr = array();
		$arr["ReturnCode"] = 0;		
		$email =$this->input->post('email');
		$exist = $this->Muser->checkEmailForRegister($email);
		if($exist){
			$arr["Msg"] = "该邮箱已注册";			
		}else{
			$arr["ReturnCode"] = 1;
		}
		echo json_encode($arr);
	}
	// 验证注册手机号
	function checkPhoneForRegister() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		
		$phone = $this->input->post ( 'phone' );
		
		// 验证如果已注册，则不允许注册
		$exists = $this->Muser->checkPhoneForRegister ( $phone );
		if ($exists) {
			$arr ["Msg"] = "该手机号码已注册";
			echo json_encode ( $arr );
			exit ();
		}
		
		// 如果没注册，则写入手机号
		$exists = $this->Muser->checkPhoneExists ( $phone );
		if ($exists == false) {
			$row = $this->Muser->registerPhone ( $phone );
			if ($row <= 0) {
				$arr ["Msg"] = "写入手机号失败";
				echo json_encode ( $arr );
				exit ();
			}
		}
		
		// 写入成功
		$arr ["ReturnCode"] = 1;
		echo json_encode ( $arr );
		exit ();
	}
	
	function checkPhoneForModify() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
	
		$phone = $this->input->post ( 'phone' );
	
		// 验证如果已注册，则不允许注册
		$exists = $this->Muser->checkPhoneForRegister ( $phone );
		if ($exists) {
			$arr ["Msg"] = "手机号码已存在";
			echo json_encode ( $arr );
			exit ();
		}
		
		$arr ["ReturnCode"] = 1;
		echo json_encode ( $arr );
		exit ();
	}
	// 验证短信验证码
	function checkSmsCode() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$phone = $this->input->post ( 'phone' );
		$smscode = $this->input->post ( 'code' );
		$exists = $this->Muser->checkSmsCode ( $phone, $smscode );
		if ($exists)
			$arr ["ReturnCode"] = 1;
		else
			$arr ["Msg"] = "验证码不匹配";
		echo json_encode ( $arr );
	}
	function checkModifyCode(){
		$arr = array();
		$arr["ReturnCode"] = 0;
		$phone = $this->input->post('phone');
		$code = $this->input->post('code');
		$exists = $this->Muser->checkModifyCode($phone,$code);
		if($exists)
			$arr["ReturnCode"] = 1;
		else 
			$arr["Msg"] = "验证码不匹配";
		echo json_encode($arr);
	}
	function updateModifyPhone(){
		$id = $this->session->userdata['id'];
		$this->Muser->updateModifyPhone($id);
		$arr = array(
			"ReturnCode" => 1
		);
		echo json_encode($arr);
	}
	// 发送短信验证码
	function sendSmsForModify() {
		/*
		 * 短信接口 http://www.dxton.com/ 登陆后下载接口 登陆账号密码 crdsnowman 123456
		*/
		$phone = $this->input->post ( 'phone' );
		$code = rand ( 1000, 9999 );
	
		//$code = "1234";
	
		$target = "http://www.dxton.com/webservice/sms.asmx/Submit";
		// 替换成自己的测试账号,参数顺序和wenservice对应
		//$post_data = "account=crdsnowman&password=123456&mobile=$phone&content=" . rawurlencode ( "您的验证码是：$code 。请不要把验证码泄露给其他人。如非本人操作，可不用理会！" );
		$post_data = "account=crdsnowman&password=8024519&mobile=$phone&content=" . rawurlencode ( "您的验证码是：$code 。请不要把验证码泄露给其他人。如非本人操作，可不用理会！" );
	
		$id = $this->session->userdata['id'];
	
		$this->Muser->saveModifyPhone ($id, $phone, $code );
	
		echo $gets = $this->Post ( $post_data, $target );
	
		// 请自己解析$gets字符串并实现自己的逻辑
		// <result>100</result>表示成功,其它的参考文档
	}
	// 发送短信验证码
	function sendSms() {
		/*
		 * 短信接口 http://www.dxton.com/ 登陆后下载接口 登陆账号密码 crdsnowman 123456
		 */
		$phone = $this->input->post ( 'phone' );
		$code = rand ( 1000, 9999 );
		
		//$code = "1234";
		
		$target = "http://www.dxton.com/webservice/sms.asmx/Submit";
		// 替换成自己的测试账号,参数顺序和wenservice对应
		//$post_data = "account=crdsnowman&password=123456&mobile=$phone&content=" . rawurlencode ( "您的验证码是：$code 。请不要把验证码泄露给其他人。如非本人操作，可不用理会！" );
		$post_data = "account=crdsnowman&password=8024519&mobile=$phone&content=" . rawurlencode ( "您的验证码是：$code 。请不要把验证码泄露给其他人。如非本人操作，可不用理会！" );
		
		
		
		$this->Muser->updateCheckCode ( $phone, $code );
		
		echo $gets = $this->Post ( $post_data, $target );
		
		// 请自己解析$gets字符串并实现自己的逻辑
		// <result>100</result>表示成功,其它的参考文档
	}
	function Post($data, $target) {
		$url_info = parse_url ( $target );
		$httpheader = "POST " . $url_info ['path'] . " HTTP/1.0\r\n";
		$httpheader .= "Host:" . $url_info ['host'] . "\r\n";
		$httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
		$httpheader .= "Content-Length:" . strlen ( $data ) . "\r\n";
		$httpheader .= "Connection:close\r\n\r\n";
		// $httpheader .= "Connection:Keep-Alive\r\n\r\n";
		$httpheader .= $data;
		
		$fd = fsockopen ( $url_info ['host'], 80 );
		fwrite ( $fd, $httpheader );
		$gets = "";
		while ( ! feof ( $fd ) ) {
			$gets .= fread ( $fd, 128 );
		}
		fclose ( $fd );
		return $gets;
	}
	function getUserInfo() {
		$arr = $this->getUserArray ();
		
		
		
		echo json_encode ( $arr );
	}
	function getUserArray() {
        $id = $this->session->userdata ['id'];
		$arr = $this->Muser->getById ( $id );
		return $arr;
	}
	function checkBirthDay() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		
		$birthday = $this->input->post ( 'birthday' );
		$dateNow = date ( 'Y-m-d' );
		if ($birthday > $dateNow) {
			$arr ["Msg"] = "生日无效";
			echo json_encode ( $arr );
			exit ();
		}
		
		$arr ["ReturnCode"] = 1;
		echo json_encode ( $arr );
	}
	// 档案维护
	function archive() {
		$province = $this->Mprovince->getAll ();
		$data ["province"] = $province;
        $data['title'] = '注册引导页';
		$this->load->view ( 'z_archive', $data );
	}
	function saveArchiveOne() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$sex = $this->input->post ( 'sex' );
		$realname = $this->input->post ( 'realname' );
		$birthday = $this->input->post ( 'birthday' );
		$provinceid = $this->input->post ( 'provinceid' );
		$cityid = $this->input->post ( 'cityid' );
		$jgprovinceid = $this->input->post ( 'jgprovinceid' );
		$jgcityid = $this->input->post ( 'jgcityid' );
		$phone = $this->input->post ( 'phone' );
		$qq = $this->input->post ( 'qq' );
		$id = $this->session->userdata ['id'];
		// echo "$sex--$realname--$birthday--$provinceid--$cityid--$jgprovinceid--$jgcityid--$phone--$qq--$id";
		// exit;
		$this->Muser->saveArchiceOne ( $id, $realname, $birthday, $provinceid, $cityid, $jgprovinceid, $jgcityid, $phone, $qq, $sex );
        $this->session->unset_userdata ( 'userinfo' );
        $arr ['ReturnCode'] = 1;
		echo json_encode ( $arr );
	}
	function archivet() {
		$this->load->view ( 'archivet' );
	}
	function saveArchiveTwo() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$high = $this->input->post ( 'high' );
		$weight = $this->input->post ( 'weight' );
		$shoesize = $this->input->post ( 'shoesize' );
		$chestsize = $this->input->post ( 'chestsize' );
		$waistsize = $this->input->post ( 'waistsize' );
		$buttsize = $this->input->post ( 'buttsize' );
		
		$worknature = $this->input->post ( 'worknature' );
		$modeltype = $this->input->post ( 'modeltype' );
		$modelstyle = $this->input->post ( 'modelstyle' );
		$modelhair = $this->input->post ( 'modelhair' );
		$modelskin = $this->input->post ( 'modelskin' );
		$modelbody = $this->input->post ( 'modelbody' );
		
		$worknature = "/" . trim ( $worknature, '/' ) . "/";
		$modeltype = "/" . trim ( $modeltype, '/' ) . "/";
		$modelstyle = "/" . trim ( $modelstyle, '/' ) . "/";
		$modelhair = "/" . trim ( $modelhair, '/' ) . "/";
		$modelskin = "/" . trim ( $modelskin, '/' ) . "/";
		$modelbody = "/" . trim ( $modelbody, '/' ) . "/";
		
		$id = $this->session->userdata ['id'];
		
		/*
		 * $arr["Msg"] = "$id,$high,$weight,$shoesize,$chestsize,$waistsize,$buttsize, $worknature,$modeltype,$modelstyle,$modelhair,$modelskin,$modelbody"; echo json_encode($arr); exit;
		 */
		
		$this->Muser->saveArchiveTwo ( $id, $high, $weight, $shoesize, $chestsize, $waistsize, $buttsize, $worknature, $modeltype, $modelstyle, $modelhair, $modelskin, $modelbody );
		$arr ["ReturnCode"] = 1;
		echo json_encode ( $arr );
	}
	function experience() {
		$this->load->view ( 'z_experience' );
	}
	function uploadImg() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$picname = $_FILES ['mypic'] ['name'];
		$picsize = $_FILES ['mypic'] ['size'];
		if ($picname != "") {
			if ($picsize > 1024000 * 3) {
				$arr ["Msg"] = '图片大小不能超过3M';
				echo json_encode ( $arr );
				exit ();
			}
			$type = strstr ( $picname, '.' );
			if ($type != ".gif" && $type != ".jpg" && $type != ".png" && $type != ".jpeg") {
				$arr ["Msg"] = '图片格式不对！';
				echo json_encode ( $arr );
				exit ();
			}
			$rand = rand ( 100, 999 );
			$pics = $this->session->userdata ['id'] . "_" . date ( "YmdHis" ) . $rand . $type;
			// 上传路径
			$pic_path = "./uploadimg/";
			if (is_dir ( $pic_path ) == false) {
				mkdir ( $pic_path, 0777 );
			}
			$pic_path .= $pics;
			move_uploaded_file ( $_FILES ['mypic'] ['tmp_name'], $pic_path );
		}
		$size = round ( $picsize / 1024, 2 );
		$arr = array (
				'ReturnCode' => 1,
				'name' => $picname,
				'src' => base_url()."uploadimg/" . $pics,
				'size' => $size 
		);
		echo json_encode ( $arr );
	}
	// 保存头像路径
	function saveArchiveHead() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$arr ["Msg"] = "";
		$this->delHead ();
		$id = $this->session->userdata ['id'];
		$headurl = $this->input->post ( 'headurl' );
		$this->Muser->updateHeadUrl ( $id, $headurl );
		$arr ["ReturnCode"] = 1;
		echo json_encode ( $arr );
	}
	function delHead() {
		try {
			$arr = $this->getUserArray ();
			$headurl = $arr ['headurl'];
			if (! empty ( $headurl )) {
				if (file_exists ( $headurl ))
					unlink ( $headurl );
			}
		} catch ( Exception $e ) {
		}
	}
	function card() {
		$this->load->view ( 'z_card' );
	}
	function saveExperience() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		
		$id = $this->session->userdata ['id'];
		$activity = $this->input->post ( 'activity' );
		$training = $this->input->post ( 'training' );
		$competition = $this->input->post ( 'competition' );
		$reward = $this->input->post ( 'reward' );
		
		$this->Muser->saveExperience ( $id, $activity, $training, $competition, $reward );
		
		$arr ["ReturnCode"] = 1;
		echo json_encode ( $arr );
	}
	function saveCard() {
		$arr = array ();
		$arr ["ReturnCode"] = 0;
		$id = $this->session->userdata ['id'];
		$cardid = $this->input->post ( 'cardid' );
		$cardcontent = $this->input->post ( 'cardcontent' );
		$this->Muser->saveCard ( $id, $cardid, $cardcontent );
		$arr ["ReturnCode"] = 1;
		echo json_encode ( $arr );
	}
	function getCardTable() {
		$arr = array ();
		$sid = $this->session->userdata ['id'];
		if (empty ( $sid )) {
			$arr ["ReturnCode"] = 0;
			echo json_encode ( $arr );
			exit ();
		}
		
		$cardid = $this->input->post ( 'cardid' );
		$lastcardid = $this->input->post ( 'lastcardid' );
		$arrUser = $this->getUserArray ();
		
		if ($arrUser ['cardid'] != $lastcardid) {
			$imgsrcs = $this->input->post ( 'imgsrcs' );
			$imgsrcs = trim ( $imgsrcs, ";" );
			$arrImg = explode ( ";", $imgsrcs );
			foreach ( $arrImg as $value ) {
				
				if (! empty ( $value ) && !strpos($value, "images/")) {
					if (file_exists ( $value ))
						unlink ( $value );
				}
			}
		}
		
		$table = "";
		if ($cardid == $arrUser ["cardid"] && $arrUser ["cardcontent"] != "") {
			$table = $arrUser ["cardcontent"];
		} else if ($cardid == "1")
			$table = $this->getTable1 ();
		else if ($cardid == "2")
			$table = $this->getTable2 ();
		else if ($cardid == "3")
			$table = $this->getTable3 ();
		else if ($cardid == "4")
			$table = $this->getTable4 ();
		else if ($cardid == "5")
			$table = $this->getTable5 ();
		
		$arr ["card"] = $table;
		echo json_encode ( $arr );
		exit ();
	}
	function getTable1() {
		$result = "
				<table class='tablecardone'>
						<tr>
							<td class='spe' rowspan='2'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link' src='images/add.png'></td>
							<td class='small'><img class='link' src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link' src='images/add.png'></td>
							<td class='small'><img class='link' src='images/add.png'></td>
						</tr>
					</table>
				";
		return $result;
	}
	function getTable2() {
		$result = "<table class='tablecardtwo'>
						<tr>
							<td class='small'><img class='link'
								src='images/add.png'></td>
							<td class='spe' colspan='3' rowspan='2'><img class='link'
								src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link'
								src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link'
								src='images/add.png'></td>
						</tr>
					</table>";
		return $result;
	}
	function getTable3() {
		$result = "<table class='tablecardthree'>
						<tr>
							<td class='small'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link'
								src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link'
								src='images/add.png'></td>
						</tr>
					</table>";
		return $result;
	}
	function getTable4() {
		$result = "<table class='tablecardfour'>
						<tr>
							<td colspan='2' class='spe'><img class='link'
								src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link'
								src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link'
								src='images/add.png'></td>
						</tr>
					</table>";
		return $result;
	}
	function getTable5() {
		$result = "<table class='tablecardfive'>
						<tr>
							<td class='spe' colspan='2' rowspan='3'><img class='link'
								src='images/add.png'></td>
							<td class='small'><img class='link' src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link' src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link' src='images/add.png'></td>
						</tr>
						<tr>
							<td class='small'><img class='link' src='images/add.png'></td>
							<td class='small'><img class='link' src='images/add.png'></td>
							<td class='small'><img class='link' src='images/add.png'></td>
						</tr>
					</table>";
		return $result;
	}
	function show() {
        $id = isset($this->session->userdata ['id'])?$this->session->userdata ['id'] :'';
        if(empty($id)){
            show_error('参数错误', 500 );
        }
        $this->setUserCodeImg($id);
		$arr = $this->getUserArray ($id);
		$data ['user'] = $arr;
        $data['icon'] = $this->UserIconStatus($id);
        $data['memberMethod'] = 'madouk';
		$this->load->view ( 'show', $data );
	}

    function ajaxSupport()
    {
        $uid = $this->input->post('id');
        $supportUid = isset($this->session->userdata ['id'])?(int)$this->session->userdata ['id'] :0;
        $check = $this->Msupport->checkSupport($uid,$supportUid);

        if(!$supportUid){
            $data['msg'] = '请先登陆';
            $data['statusCode'] = 2;
            echo json_encode($data);
            exit;
        }

        if($check){
            $data['msg'] = '已支持过';
            $data['statusCode'] = 2;
            echo json_encode($data);
            exit;
        }
        $return = $this->Msupport->insertSupport($uid,$supportUid);
        if($return){
            $data['msg'] = '成功';
            $data['statusCode'] = 1;
        }else{
            $data['msg'] = '失败';
            $data['statusCode'] = 3;
        }
        echo json_encode($data);
    }

	function showperson(){
		$id = $this->input->get('id');
        $uid = isset($this->session->userdata ['id'])?$this->session->userdata ['id'] :0;
        if(empty($id)){
            show_error('参数错误', 500 );
        }
        $this->setUserCodeImg($id);
		$this->Muser->modifyPV($id);
		$arr = $this->Muser->getById ( $id );
        $arr ['user'] = $arr;
        $arr['icon'] = $this->UserIconStatus($id);
        $arr['zan'] = $this->Msupport->checkSupport($id,$uid);
        $arr['memberMethod'] = 'madouk';
		$this->load->view ( 'showperson', $arr );
	}

	function search(){
		$this->load->view('search');
	}
	function loginemail(){
		$this->load->view('emailtip');
	}
	function malbumshow(){
		$uid = $this->session->userdata['id'];

        if(empty($uid)){
            show_error('参数错误', 500 );
        }
        $this->setUserCodeImg($uid);
		$this->Malbum->initAlbum($uid);
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
        $data['icon'] = $this->UserIconStatus($uid);
        $data['memberMethod'] = 'photo';
		$data ['ht'] = $ht;
		$this->load->view ( 'albumshow', $data );
	}

    function bumshow(){
        $uid = $this->input->get('id');
        $id = isset($this->session->userdata ['id'])?$this->session->userdata ['id'] :0;
        if(empty($uid)){
            show_error('参数错误', 500 );
        }
        $this->Muser->modifyPV($uid);
        $this->setUserCodeImg($uid);
        //$this->Malbum->initAlbum($uid);

        $arr['user'] = $this->Muser->getById ( $uid );
        $ht = true;
        $arr['icon'] = $this->UserIconStatus($uid);
        $arr['zan'] = $this->Msupport->checkSupport($uid,$id);
        $arr['memberMethod'] = 'photo';
        $arr ['ht'] = $ht;
        $this->load->view ( 'bumshow', $arr );
    }

	function loadAlbumDetail(){
		$uid = $this->session->userdata['id'];
		$this->Malbum->initAlbum($uid);
		$user = $this->getUserArray ();
		$data ['user'] = $user;
		$ht = true;
		$data ['ht'] = $ht;
		$albumid = $_GET["albumid"];
		$query = $this->Malbumdetail->loadAlbumDetail($albumid);
        $data['icon'] = $this->UserIconStatus($uid);
        $data['memberMethod'] = 'photo';
		$img="";
		foreach ($query as $row){
			$img.="<img src='".base_url().self::IMGPATH.$row->url."' style='padding-top:10px;' width='100%' height='100%' />";
		}
		if($img==""){
			$img="<div style='height:500px;'></div>";
		}
		$data['img'] = $img;
		$this->load->view ( 'albumdetail', $data );
	}

    function albumDetail(){
        $id = $this->input->get('id');
        $uid = $this->session->userdata['id'];
        $ht = true;
        $data ['ht'] = $ht;
        $albumid = $_GET["albumid"];
        $this->Muser->modifyPV($id);
        $query = $this->Malbumdetail->loadAlbumDetail($albumid);
        $data['user'] = $this->Muser->getById ( $id );
        $data['icon'] = $this->UserIconStatus($id);
        $data['zan'] = $this->Msupport->checkSupport($id,$uid);
        $data['memberMethod'] = 'photo';
        $img="";
        foreach ($query as $row){
            $img.="<img src='".base_url().self::IMGPATH.$row->url."' style='padding-top:10px;' width='100%' height='100%' />";
        }
        if($img==""){
            $img="<div style='height:500px;'></div>";
        }
        $data['img'] = $img;
        $this->load->view ( 'albumdetail', $data );
    }
	
	function searchModel(){
		$cityid = $this->input->post('cityid');
		$modeltype = $this->input->post('modeltype');
		$minHeight = $this->input->post('minHeight');
		$maxHeight = $this->input->post('maxHeight');
		$minAge = $this->input->post('minAge');
		$maxAge = $this->input->post('maxAge');
		$sex = $this->input->post('sex');
		$age = $this->input->post('age');
		
		//echo "$cityid-$modeltype-$minHeight-$maxHeight-$min"
		
		 $query = $this->Muser->searchModel($cityid,$modeltype,$minHeight,$maxHeight,$minAge,$maxAge,$sex,$age);
		//echo $result;
		//exit;
		$result = "";
		foreach ($query as $row){
			$modeltypename = $this->Muser->getModelTypeName($row->modeltype);
			$imgtx = $row->headurl;
			if(!isset($imgtx) || $imgtx==""){
				$imgtx = "images/touxiang_img.jpg";
			}
			$result.="<li>
					<div class='mingpian'>
						<div class='mingpian_img'>
							<a href='index.php/login/showperson?id=$row->id' target='_blank'><img src='$imgtx' width='125' height='125' /></a>
						</div>
						<div class='mingpian_name'>
							<a href='index.php/login/showperson?id=$row->id' target='_blank'>$row->realname</a>
							<div class='mingpian_renzheng'>";
			if($row->idcardstatus==3)
						$result.="<img src='images/sfrz.jpg' width='58' height='22' />";
			$result.="
							</div>
						</div>
						<div class='mingpian_js'>
							<ul>
								<li>所在地：$row->cityname</li>
								<li>人气：$row->pvcount</li>
								<li>年龄：<span>$row->age 岁</span></li>
								<li>身高：<span>$row->high CM</span></li>
							</ul>
						</div>
						<div class='mingpian_js'>职 业：$modeltypename</div>
					</div>
				</li>";
		}
		echo $result;
	}
	
	
	
	function create_guid() {
	
		$charid = strtoupper(md5(uniqid(mt_rand(), true)));
	
		$hyphen = chr(45);// "-"
	
		$uuid = chr(123)// "{"
	
		.substr($charid, 0, 8).$hyphen
	
		.substr($charid, 8, 4).$hyphen
	
		.substr($charid,12, 4).$hyphen
	
		.substr($charid,16, 4).$hyphen
	
		.substr($charid,20,12)
	
		.chr(125);// "}"
	
		$uuid = trim($uuid,"{");
		$uuid = trim($uuid,"}");
		return $uuid;
	
	}
	
	public function reSendMail(){
		$email = $this->input->post('email');
		$checkcode = $this->input->post('checkcode');
		$content = $this->getRegEmailContent($email,$checkcode);
		$this->sendMail($email,$content);
	}
	
	public function sendMail($to,$content,$subject='麻豆注册',$myEmail='madou'){
		$this->load->library('email');            //加载CI的email类

		//以下设置Email参数
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'smtp.163.com';
        $config['smtp_user'] = 'madoumaster@163.com';
        $config['smtp_pass'] = 'madou123';
        $config['smtp_port'] = '25';
        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        $this->email->initialize($config);

		//以下设置Email内容
		$this->email->from('madoumaster@163.com', $myEmail);
		$this->email->to($to);
		$this->email->subject($subject);
		$this->email->message($content);
		//$this->email->attach('application\controllers\1.jpeg');			//相对于index.php的路径
	
		$this->email->send();
		return;
		//echo $this->email->print_debugger();
	}

    function sendHtmlMail()
    {
        $email = $this->input->post('email');
        $myEmail = $this->input->post('myEmail');
        $emailTitle = $this->input->post('emailTitle');
        $id = $this->input->post('id');

        if(empty($id)){
            $uid = $id = $this->session->userdata ['id'];
        }else{
            $uid = $id;
        }
        $arr = $this->Muser->getById ( $uid );
        $content = $arr['cardcontent'];

        $this->sendMail($email,$content,$emailTitle,$myEmail);
    }

    /**
     * 照片墙
     */
    function MyphotoWall()
    {
        $id = isset($this->session->userdata ['id'])?$this->session->userdata ['id'] :0;
        if(empty($id)){
            show_error('参数错误', 500 );
        }
        $this->setUserCodeImg($id);
        $arr['pageNum'] = $this->input->get('pageNum') ? (int)$this->input->get('pageNum') : 2;
        $arr['memberMethod'] = 'wall';
        $arr['user'] = $this->getUserArray ($id);
        $arr['icon'] = $this->UserIconStatus($id);
        $arr['data'] = $this->Malbumdetail->getAllPhoto($id,20,'id desc');

        $this->load->view ( 'photoWall',$arr );
    }

    /**
     * 照片墙
     */
    function photoWall()
    {
        $uid = $this->input->get('id');
        $id = isset($this->session->userdata ['id'])?$this->session->userdata ['id'] :0;

        if(empty($uid)){
            show_error('参数错误', 500 );
        }
        $this->Muser->modifyPV($uid);
        $this->setUserCodeImg($uid);
        $arr['pageNum'] = $this->input->get('pageNum') ? (int)$this->input->get('pageNum') : 2;
        $arr['memberMethod'] = 'wall';
        $arr['user'] = $this->Muser->getById ( $uid );
        $arr['icon'] = $this->UserIconStatus($uid);
        $arr['zan'] = $this->Msupport->checkSupport($uid,$id);
        $arr['data'] = $this->Malbumdetail->getAllPhoto($uid,20,'id desc');


        $this->load->view ( 'photoWall',$arr );
    }

    /**
     * ajax加载照片墙
     */
    function ajaxAllPhono()
    {
        $data = $this->Malbumdetail->getAllPhoto(20,'id desc');

        $html = '';
        if(!empty($data['data'])){

            foreach($data['data'] as $value){
                $html .= '<div class="item masonry_brick">
                        <div class="item_t">
                            <div class="img">
                                <a href="javascript:;"><img width="210" height="'.$value['height'].'" alt="相册" src="'.base_url().'uploads/'.$value['url'].'" /></a>
                            </div>
                            <div class="title"><span>js lazyload实现网页图片延迟加载特效</span></div>
                        </div>
                        <div class="item_b clearfix">
                            <div class="items_likes fl">
                                <a href="javascript:;" class="like_btn" onclick="markerLike('.$value['id'].',this);"></a>
                                <em class="bold">'.$value['like_count'].'</em>
                            </div>
                            <div class="items_comment fr"><a href="">评论</a><em class="bold">(0)</em></div>
                        </div>
                    </div>';
            }
        }
        echo $html;
    }


    function markLike()
    {
        $photoId = $this->input->post('photoId');
        $uid = $this->session->userdata ['id'];

        $check = $this->Mphotodetail->checkPhotoMake($photoId,$uid);

        if($check){
            $this->Malbumdetail->markLike($uid,$photoId);
            $data['statusCode'] = 1;
            $data['msg'] = '喜欢成功';
        }else{
            $data['statusCode'] = 2;
            $data['msg'] = '不能重复喜欢';
        }

        echo json_encode($data);
    }

    /**
     * 获取幻灯
     */
    function getHuanDeng($advertise_id)
    {
        return $this->Madvertisepic->getAdvertisePic($advertise_id,'sort desc');
    }

    /**
     * 微访谈
     */
    function tinyInterview()
    {

        $id = $this->input->get('id');
        $uid = isset($this->session->userdata ['id'])?$this->session->userdata ['id'] :0;

        if(empty($id)){
            show_error('参数错误', 500 );
        }
        $this->Muser->modifyPV($id);
        $arr['girlContent'] = $this->Marticle->getArticleByUid($id);
        $arr['user'] = $this->Muser->getById ( $id );
        $arr['icon'] = $this->UserIconStatus($id);
        $arr['zan'] = $this->Msupport->checkSupport($id,$uid);
        $arr['memberMethod'] = 'interview';
        $this->load->view('interview',$arr);
    }

    /**
     * 微访谈
     */
    function myInyInterview()
    {
        $id = isset($this->session->userdata ['id']) ? (int)$this->session->userdata ['id'] :0;

        if(empty($id)){
            show_error('参数错误', 500 );
        }

        $arr['girlContent'] = $this->Marticle->getArticleByUid($id);
        $arr['icon'] = $this->UserIconStatus($id);
        $arr['memberMethod'] = 'interview';
        $arr['user'] = $this->getUserArray ();


        $this->load->view('interview',$arr);
    }

    function ajaxVote()
    {
        $id = (int)$this->input->post('id');
        $interview = (int)$this->input->post('interview');
        $uid = isset($this->session->userdata ['id']) ? (int)$this->session->userdata ['id'] :0;

        if(!$uid){
            $data['statusCode'] = 2;
            $data['msg'] = '请先登陆';
            echo json_encode($data);
            exit;
        }

        if(empty($id)){
            $data['statusCode'] = 2;
            $data['msg'] = '参数错误';
            echo json_encode($data);
            exit;
        }

        $check = $this->Minterviewvote->checkVote($id,$uid,$interview);
        if($check){
            $data['statusCode'] = 2;
            $data['msg'] = '已投过票';
            echo json_encode($data);
            exit;
        }

        $this->Minterviewvote->insertVote($id,$uid,$interview);
        $this->Muser->voteInterview($id);

        $data['statusCode'] = 1;
        $data['msg'] = '成功';
        echo json_encode($data);

    }

    function UserIconStatus($uid)
    {
        $icon = $this->Muserimg->getUserIcon($uid);

        $html = '';
        $baseUrl = base_url();
        $mourl = 'images/icon_star.png';
        if(!empty($icon)){

            for($i=0;$i<5;$i++){
                $url = isset($icon[$i]['url']) ? $icon[$i]['url'] : $mourl;
                $name = isset($icon[$i]['name']) ? $icon[$i]['name'] : '';
                $html .= '<a href="javascript:void(0)"><img src="'.$baseUrl.$url.'" title="'.$name.'"/> </a>';
            }
        }else{
            $html = '<a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>
            <a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>
            <a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>
            <a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>
            <a href="javascript:void(0)"><img src="'.$baseUrl.$mourl.'" /> </a>';
        }

        return $html;
    }

    /**
     * @param $uid
     */
    function setUserCodeImg($uid)
    {
        $usre = $this->Muser->getUserByUid ($uid);
        $dir = dirname(__FILE__);
        $imgUrl = $dir.'/../../uploads/'.$usre['headurl'];

        if(is_file($imgUrl)){
            $codeImg = $imgUrl;
        }else{
            $codeImg = $dir.'/../../images/touxiang_img.jpg';
        }

        $url = base_url().'index.php/login/showperson?id='.$usre['id'];
        if(empty($usre['codeimg'])){
            $userCodeImg = $this->getUserCode($url,$codeImg,$usre['id']);
            $this->Muser->setUserCode($uid,$userCodeImg);
        }
    }

    function getUserCode($url,$imgUrl,$uid=0)
    {
        //生成中间带logo的二维码
        //$this->load->library('phpqrcode/phpqrcode');
        $dir = dirname(__FILE__);
        include($dir.'/../../system/libraries/phpqrcode/phpqrcode.php');
        $dataTime = date('Ymd',time());
        $codeDir = $dir.'/../../imgCode/'.$dataTime;
        if(!file_exists($codeDir)){
            @mkdir($codeDir);
        }

        $QR = $codeDir.'/'.$uid.'_xiangyang.png';
        $value=$url;
        $userCodeImg = $codeDir.'/'.$uid.'.png';
        $errorCorrectionLevel = 'L';
        $matrixPointSize = 10;
        QRcode::png($value, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

        $logo = $imgUrl;

        if($logo !== FALSE)
        {

            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);
            $QR_height = imagesy($QR);
            $logo_width = imagesx($logo);
            $logo_height = imagesy($logo);
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        }
        $return = imagepng($QR,$userCodeImg);
        if($return){
            return $dataTime.'/'.$uid.'.png';
        }else{
            return '';
        }
    }

}
?>